package com.hzdh.account.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

public class DateUtils {
	/**
	 * 日期格式化，默认日期格式日期格式化，默认日期格式yyyy-MM-dd
	 * 
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getFormatDate(Date date, String format) {
		if (format == null) {
			format = "yyyy-MM-dd";
		}
		// 特殊处理 这种情况一般不会需要返回格式化的数据
		if (date == null || date.getTime() == 0L) {
			return "";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);
	}

	/**
	 * 将符合格式的时间字符串转换成Date
	 * 
	 * @param str
	 * @return Date
	 * 
	 * @author
	 */
	public static Date strToFullDate(String str) {
		if (str == null || str.intern().length() == 0) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将符合格式的时间字符串转换成Date
	 * 
	 * @param str
	 * @return Date
	 * 
	 * @author
	 */
	public static Date strToDate(String str) {
		if (str == null || str.intern().length() == 0) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
		Date date = null;
		try {
			date = format.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 将符合格式的时间字符串转换成Date
	 * 
	 * @param str
	 * @return Date
	 * 
	 * @author
	 */
	public static Date stringToDate(String str) {
		if (str == null || str.intern().length() == 0) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		Date date = null;
		try {
			if (str.length() == 14) {
				date = format.parse(str);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 加上多少个月
	 * 
	 * 
	 */
	public static Date plusMonth(Date date, int month) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");// 格式化对象
		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.setTime(date);// 设置当前日期
		calendar.add(Calendar.MONTH, month);// 月份减一

		String str = sdf.format(calendar.getTime());
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 加上多少天
	 * 
	 * @param date
	 * @return
	 */
	public static Date plusDay(Date date, int day) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");// 格式化对象
		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.setTime(date);// 设置当前日期
		calendar.add(Calendar.DAY_OF_YEAR, day);// 月份减一

		String str = sdf.format(calendar.getTime());
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date getFirstDayOfTheMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);// 设置当前日期
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	public static Date getFirstDayOfTheMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 获取指定日期的当天00:00:00
	 * 
	 * @return
	 */
	public static Date getDayZero(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	/**
	 * 将符合格式的时间字符串转换成Date
	 *
	 * @param str
	 * @return Date
	 *
	 * @author
	 */
	public static Date formatString2Date(String str, String format) {
		if (str == null || str.intern().length() == 0) {
			return null;
		}
		if (StringUtils.isBlank(format)) {
			format = "yyyy-MM-dd HH:mm:ss";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static String getYearForString() {
		Calendar calendar = Calendar.getInstance();
		return String.valueOf(calendar.get(Calendar.YEAR));
	}

	/*
	 * @desc:指定日期加上多少分钟
	 */
	public static Date addDateMinut(Date day, int x) {
		// System.out.println("after:" + format.format(day)); //显示更新后的日期
		Date date = null;
		// 引号里面个格式也可以是 HH:mm:ss或者HH:mm等等，很随意的，不过在主函数调用时，要和输入的变
		Calendar cal = Calendar.getInstance();
		cal.setTime(day);
		cal.add(Calendar.MINUTE, x);// 24小时制

		date = cal.getTime();
		// System.out.println("after:" + format.format(date)); //显示更新后的日期

		return date;

	}

	/*
	 * @desc:字符串转成指定格式的日期
	 */
	public static Date getFormatDateByString(String date, String format) throws ParseException {
		if (format == null) {
			format = "yyyy-MM-dd";
		}
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		return fmt.parse(date);
	}

	/*
	 * @desc:字符串转成指定格式的日期
	 */
	public static String getFormatDateByDate(Date date, String format) throws ParseException {
		if (null == format) {
			format = "yyyy-MM-dd";
		}
		SimpleDateFormat fmt = new SimpleDateFormat(format);
		if (null == date) {
			return null;
		} else {
			return fmt.format(date);
		}
	}

	public static void main(String[] args) throws ParseException {
		Date date = new Date();
		String str = DateUtils.getFormatDate(date, "yyyyMMdd");
		System.out.println(str);

	}

	public static int getIntervalDays(Date fDate, Date oDate) {

		Calendar aCalendar = Calendar.getInstance();

		aCalendar.setTime(fDate);

		int day1 = aCalendar.get(Calendar.DAY_OF_YEAR);

		aCalendar.setTime(oDate);

		int day2 = aCalendar.get(Calendar.DAY_OF_YEAR);

		return day2 - day1;

	}
}
